<!-- 正文开始 -->
<div class="layui-fluid">
  <div class="layui-card">
    <div class="layui-card-body">
      <!-- 头部操作栏 -->
      <form class="layui-form">
        <div class="layui-form lay-header">
          <div class="toolbar">
            <div class="layui-form-item lay-query-content">
              <div class="lay-query-box">
                <div class="query-lt">
                  <div class="layui-inline">
                    <label class="layui-form-label">所属工序：</label>
                    <div class="layui-input-inline">
                      <select name="processId" id="selectProcessId">
                        <option value="">选择工序</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label">质检记录编号：</label>
                    <div class="layui-input-inline">
                      <input
                        name="code"
                        class="layui-input"
                        type="text"
                        placeholder="请输入质检任务编号"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label">工单编号：</label>
                    <div class="layui-input-inline">
                      <input
                        name="workOrderCode"
                        class="layui-input"
                        type="text"
                        placeholder="请输入工单编号"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">报工单编号：</label>
                    <div class="layui-input-inline">
                      <input
                              name="reportOrderCode"
                              class="layui-input"
                              type="text"
                              placeholder="请输入报工单编号"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">质检分类：</label>
                    <div class="layui-input-inline">
                      <select name="qualityTestingCategoryId">
                        <option value="">全部</option>
                        <option value="1">化学检测</option>
                        <option value="2">外观检测</option>
                        <option value="3">物理检测</option>
                        <option value="4">其他检测</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">质检类型：</label>
                    <div class="layui-input-inline">
                      <select name="qcQualityTypeId">
                        <option value="">全部</option>
                        <option value="1">来料质检(IQC)</option>
                        <option value="2">工序质检(IPQC)</option>
                        <option value="3">成品质检(FQC)</option>
                        <option value="4">出库检</option>
                      </select>
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">任务添加时间：</label>
                    <div class="layui-input-inline">
                      <input name="createTime" id="createTime" class="layui-input date-icon" type="text"
                             placeholder="请选择任务添加时间" style="width: 200px" readonly/>
                    </div>
                    <input name="startCreateTime" id="sCreateTime" type="hidden"/>
                    <input name="endCreateTime" id="eCreateTime" type="hidden"/>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">送检人：</label>
                    <div class="layui-input-inline">
                      <input
                              name="inspector"
                              class="layui-input"
                              type="text"
                              placeholder="请输入送检人"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">认领人：</label>
                    <div class="layui-input-inline">
                      <input
                              name="getName"
                              class="layui-input"
                              type="text"
                              placeholder="请输入认领人"
                      />
                    </div>
                  </div>
                  <div class="layui-inline">
                    <label class="layui-form-label w-auto">车间：</label>
                    <div class="layui-input-inline">
                      <input
                              name="workshopName"
                              class="layui-input"
                              type="text"
                              placeholder="请输入车间"
                      />
                    </div>
                  </div>
                </div>
                <div class="query-rt">
                  <div class="layui-inline">
                    <button
                      class="layui-btn icon-btn color-green"
                      lay-filter="formSubSearchLog"
                      lay-submit
                      type="button"
                    >
                      查询
                    </button>
                    <button
                      class="layui-btn icon-btn color-white"
                      lay-filter="resets"
                      lay-submit
                      type="button"
                    >
                      重置
                    </button>
                  </div>
                </div>
              </div>
            </div>
          </div>
          <!--                    <div class="lay-btn-rows">-->
          <!--                        <div class="lay-btn-box">-->
          <!--                            <div class="lay-btn-lt">-->
          <!--                                <button id="taskQualityRecordBtnAdd" class="layui-btn icon-btn color-green"-->
          <!--                                        type="button">-->
          <!--                                    添加检测记录-->
          <!--                                </button>-->
          <!--                            </div>-->
          <!--                            <div class="lay-btn-rt">-->
          <!--                            </div>-->
          <!--                        </div>-->
          <!--                    </div>-->
        </div>
      </form>
      <!-- 表格 -->
      <table
        class="layui-table"
        id="qualityQualityRecordTable"
        lay-filter="qualityQualityRecordTable"
      ></table>
    </div>
  </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="qualityTestingRecordTableBar">
  <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="details"
    >详情</a
  >
  <!--    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>-->
  <a class="layui-btn layui-btn-xs" lay-event="print">打印</a>
</script>

<!-- js部分 -->
<script>
  layui.use(["layer", "form", "table", "admin", "laydate"], function () {
    var $ = layui.jquery;
    var layer = layui.layer;
    var form = layui.form;
    var table = layui.table;
    var config = layui.config;
    var admin = layui.admin;
    var upload = layui.upload;
    var laydate = layui.laydate;

    form.render("select");

    laydate.render({
      elem: "#createTime",
      type: "date",
      range: true,
      done: function (value, date, endDate) {
        if (value) {
          var dateValue = value.split(" - ");
          $("input[name='startCreateTime']").val(dateValue[0] + " 00:00:00");
          $("input[name='endCreateTime']").val(dateValue[1] + " 23:59:59");
        } else {
          $("input[name='startCreateTime']").val("");
          $("input[name='endCreateTime']").val("");
        }
      },
    });
    
    // 渲染表格
    var insTb = table.render({
      elem: "#qualityQualityRecordTable",
      url: config.base + "production/quality/admin/taskQualityRecord/list",
      page: true,
      cellMinWidth: 100,
      cols: [
        [
          { type: "numbers", title: "序号", align: "center" },
          { field: "sampleCode", title: "样品编码", align: "center" },
          { field: "taskCode", title: "质检任务编码", align: "center" },
          { field: "workCode", title: "所属工单/订单", align: "center" },
          { field: "planCode", title: "所属计划", align: "center" },
          {
            field: "qualityAttributes",
            title: "质检属性",
            align: "center",
            templet: (d) => {
              return d.sampleCode.indexOf("SIMP") > -1
                ? "简易检测"
                : "一物一检";
            },
          },
          { field: "qcQualityTypeName", title: "质检类型", align: "center" },
          /*, {field: 'qualityTestingNatureName', title: '质检性质', align: 'center'}*/
          { field: "categoryName", title: "质检分类", align: "center" },
          { field: "statusName", title: "检测状态", align: "center" },
          {
            field: "qualityTestingResultName",
            title: "判定结果",
            align: "center",
          },
          { field: "modifierName", title: "检验员", align: "center" },
          { field: "updateTime", title: "检出时间", align: "center" },
          {
            align: "center",
            toolbar: "#qualityTestingRecordTableBar",
            title: "操作",
            width: 200,
          },
        ],
      ],
      done: function (res, curr, count) {
        //移除按钮
      },
    });

    // 工具条点击事件
    table.on("tool(qualityQualityRecordTable)", function (obj) {
      let data = obj.data;
      if (obj.event === "details") {
        //详情
        showDetailsModel(data);
      } else if (obj.event === "print") {
        // 打印
        getPrintBarcode(data);
      }
    });

    //监听排序
    table.on("sort(qualityQualityRecordTable)", function (obj) {
      table.reload("qualityQualityRecordTable", {
        initSort: obj,
        where: {
          sort: obj.field,
          order: obj.type,
        },
      });
    });

    // 搜索
    form.on("submit(formSubSearchLog)", function (data) {
      insTb.reload({ where: data.field, page: { curr: 1 } }, "data");
    });

    // 重置搜索
    form.on("submit(resets)", (data) => {
      insTb.reload(
        { where: admin.resetSearch(data.field), page: { curr: 1 } },
        "data"
      );
    });

    // 添加按钮点击事件
    // $('#taskQualityRecordBtnAdd').click(function () {
    //     showEditModel();
    // });

    let addQualityTestingRecordTableData = [],
      addQualityTestingRecordTableListData = [];

    // 显示编辑弹窗
    // function showEditModel(data) {
    //     if (data){
    //         admin.putTempData("taskQualityRecordData",data);
    //     }else{
    //         admin.putTempData("taskQualityRecordData",null);
    //     }
    //     admin.formOpen({
    //         type: 1,
    //         area: ['80%', '92%'],
    //         offset: ['7%','17%'],
    //         style: 'font-size: 20px;',
    //         title: data ? '修改质检记录表' : '添加质检记录表',
    //         path: 'components/res/quality/taskQualityRecordForm.html',
    //         finish: function () {
    //             table.reload(insTb, {});
    //         }
    //     });
    // }

    function showDetailsModel(data) {
      admin.putTempData("taskQualityRecordData", data);
      admin.formOpen({
        type: 1,
        area: ["80%", "92%"],
        style: "font-size: 20px;",
        title: "查看详情",
        path: "components/res/quality/taskQualityRecordDetailNoEditForm.html",
        finish: function () {
          table.reload(insTb, {});
        },
      });
    }

    // 请求条码接口
    function getPrintBarcode(data) {
      layer.load(2);
      admin.req(
        "enterprise/product/admin/productPackLabel/getBarCode",
        { code: data.code },
        function (res) {
          layer.closeAll("loading");
          if (res.code == 200) {
            var newstr = `
                        <div style="padding: 20px;width: 100%;box-sizing: border-box;">
                            <div style="height: 150px;width: 100%">
                                <div style="width: 150px;float: left;height: 100%;">
                                    <img src="${res.data}" style="max-width: 100%; width: 100%;height: 100%;"/>
                                </div>
                                <div style="width: calc(100% - 150px); float: left;height: 100%; box-sizing: border-box; padding: 15px; font-size: 16px">
                                    ${data.skuCode}
                                </div>
                            </div>
                            <p style="margin-top: 5px;font-size: 16px;width: 100%;box-sizing: border-box;height: 20px; overflow: hidden"> ${data.code}</p>
                        </div>
                    `;
            printBarcode(newstr);
          } else {
            layer.msg(res.msg, { icon: 2 });
          }
        },
        "GET"
      );
    }

    // 打印条码
    function printBarcode(data) {
      admin.open({
        type: 1,
        area: ["330px", "350px"],
        offset: "65px",
        title: "打印预览",
        content: data,
        btn: ["打印", "取消"],
        yes: function () {
          let newWin = window.open("Print_window", "_blank"); //新打开一个空窗口
          newWin.document.write(data); //将表格添加进新的窗口
          newWin.document.close(); //在IE浏览器中使用必须添加这一句
          newWin.focus(); //在IE浏览器中使用必须添加这一句
          newWin.print(); //打印
          newWin.close(); //关闭窗口
        },
        btn2: function () {
          layer.closeAll();
        },
      });
    }

    // 导出excel
    $("#qualityTestingRecordBtnExport").click(function () {
      var checkRows = table.checkStatus("qualityQualityRecordTable");
      if (checkRows.data.length == 0) {
        layer.msg("请选择要导出的数据", { icon: 2 });
      } else {
        table.exportFile(insTb.config.id, checkRows.data, "xls");
      }
    });
  });
</script>
